package easy;

/**
 * 最长公共前缀
 *
 * @author WangChenyu
 * @since 2024-04-15 13:46
 */
public class LongCommonPrefix {


    public static void main(String[] args) {
        String[] arr = {"abcd","abdc","abf","af"};
        System.out.println(getLongCommonPrefix(arr));
    }
    public static String getLongCommonPrefix(String[] arr) {
        if (arr == null || arr.length == 0) {
            return "";
        }
        String maxPrefix = arr[0];
        for (String s : arr) {
            while (!s.startsWith(maxPrefix)) {
                maxPrefix = maxPrefix.substring(0,maxPrefix.length() - 1);
            }
        }
        return maxPrefix;
    }



}
